package org.buptsse.page;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.buptsse.entity.User;

import org.buptsse.ui.R;
import org.buptsse.ui.TestActivity;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ConnectionChecker;
import org.buptsse.utils.GloableInstance;
import org.buptsse.utils.HttpHelper;
import org.buptsse.utils.MD5Helper;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MyPasswordPage extends LinearLayout implements OnClickListener {
	private final String SAVE_PATH = "/mnt/sdcard/tmp/";
	private View viewpersonal;
	private User user = null;
	private Button quwen, guanzhu, fansi,inforSet,circleCreate;
	private Context context;
	private ImageView headImage = null;
	private Handler handler;
	/*=new Handler()
	{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.what==1)
			{
				pDialog.dismiss();
			}
		}
	
	};*/
	
	private EditText pwdOldEditText,pwdNew1EditText,pwdNew2EditText;
	private Button changePwdButton;
	private ProgressDialog pDialog;
	public MyPasswordPage(Context context, AttributeSet attrs ,Handler handler) {
		super(context, attrs);
		this.handler=handler;
		init();
	}
	
	public MyPasswordPage(Context context, User user,Handler handler) {
		super(context);
		this.handler=handler;
		this.context = context;
		this.user = null;
		init();
	}

	private void init() {
		LayoutInflater inflater = LayoutInflater.from(getContext());
		viewpersonal = inflater.inflate(R.layout.lmy_setting_pwd, null);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		addView(viewpersonal, params);
		pwdOldEditText=(EditText) findViewById(R.id.pwd_old);
		pwdNew1EditText=(EditText) findViewById(R.id.pwd1);
		pwdNew2EditText=(EditText) findViewById(R.id.pwd2);
		
		//修改密码按钮
		changePwdButton=(Button) findViewById(R.id.change_pwd_button);
		changePwdButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ChangePwd();
			}
		});
	}
	
	public void clearPwd()
	{
		pwdOldEditText.setText("");
		pwdNew1EditText.setText("");
		pwdNew2EditText.setText("");
	}

	public void ChangePwd()
	{
		String oldpwd=pwdOldEditText.getText().toString();
		String pwd1=pwdNew1EditText.getText().toString();
		String pwd2=pwdNew2EditText.getText().toString();
		if(!pwd1.equals(pwd2))
		{
			Message msg=handler.obtainMessage(APPConstant.NewPasswordNotSame);
			handler.sendMessage(msg);
			return;
		}
		Message msg=handler.obtainMessage(APPConstant.showWaittingDialog);
		msg.obj="正在修改密码";
		handler.sendMessage(msg);
		ChangePwdThread ct=new ChangePwdThread(context, handler, MD5Helper.MD5Encoded(oldpwd), MD5Helper.MD5Encoded(pwd2));
		ct.start();
	}
	
	class ChangePwdThread extends Thread
	{
		private String oldpwd,newpwd;
		private Handler handler;
		private Context context;
		
		public ChangePwdThread(Context context,Handler handler,String oldpwd,String newpwd)
		{
			this.context=context;
			this.handler=handler;
			this.oldpwd=oldpwd;
			this.newpwd=newpwd;
		}
		
		public void run()
		{
			if(ConnectionChecker.CheckConnection(context, handler)==false)
			{
				return;
			}
			ArrayList<NameValuePair> param=new ArrayList<NameValuePair>();
			param.add(new BasicNameValuePair("username", GloableInstance.user.getName()));
			param.add(new BasicNameValuePair("oldpassword", oldpwd));
			param.add(new BasicNameValuePair("newpassword", newpwd));
			try {
				String result=HttpHelper.sendPost(APPConstant.changePwdUrl, param);
				System.out.println("in change password result:"+result);
				if(result.contains("success"))
				{
					GloableInstance.user.setPassword(newpwd);
					Message msg=handler.obtainMessage(APPConstant.ChangePasswordFinish);
					handler.sendMessage(msg);
					return;
				}else {
					Message msg=handler.obtainMessage(APPConstant.ChangePasswordError);
					handler.sendMessage(msg);
					return;
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		Intent it = new Intent();
		switch (v.getId()) {
		
		}
	}

}
